package com.cgw.web.room.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.cgw.common.base.BaseAction;
import com.cgw.common.util.help.CookieUtils;
import com.cgw.dmplt.address.domain.Helper;
import com.cgw.dmplt.base.domain.BaseRentprice;
import com.cgw.dmplt.common.PageBean;
import com.cgw.dmplt.room.dao.RoomDao;
import com.cgw.dmplt.room.domain.Room;
import com.cgw.web.base.action.BaseDataAction;

public class RoomAction extends BaseAction {
	
	private Room room;
	private PageBean<Room> page;
	private Integer roomId;
	private String basePrice;
	HttpServletResponse response = ServletActionContext.getResponse();
	HttpServletRequest request = ServletActionContext.getRequest();

	public String getBasePrice() {
		return basePrice;
	}

	public void setBasePrice(String basePrice) {
		this.basePrice = basePrice;
	}

	public String doList(){
		RoomDao roomDao=(RoomDao)getBean("roomDao");
		try{
			if(room==null){
				room=new Room();
			}
			List<Room> roomList=roomDao.listRoom(room);
			
			Integer count=roomDao.listRoomCount(room);
			
			page=new PageBean(roomList,count,room.getCurentPage());
			
		}catch(Exception e){
			log.error("��ѯ�����б����!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * @author sylvan
	 * @date 510
	 * @return
	 */
	public String preList(){
		RoomDao roomDao=(RoomDao)getBean("roomDao");
		try{
			if(room==null){
				room=new Room();
			}
//			HttpSession session = ServletActionContext.getRequest().getSession(); 
//			if(helper.getRentPriceID()!=null){
//				if(session.getAttribute(helper.getRentPriceID())==null){
//					CookieUtils.addRentPriceIDSession(helper.getRentPriceID(),helper.getRentPriceID());
//					System.out.println("------add session ------"+helper.getRentPriceID());
//				}else{
//	//				helper.setRentPriceID(session.getAttribute(helper.getRentPriceID()).toString());
//					helper.setRentPriceID(session.getAttribute("sRentPriceID").toString());
//					System.out.println("------add session to bean------"+session.getAttribute(helper.getRentPriceID()).toString());
//				}
//			}
//			if(helper.getRoomTypeID()!=null){
//				CookieUtils.addRoomTypeSession(helper.getRoomTypeID(),helper.getRoomTypeID());
//			}
			
			
			setBasePrice(new BaseDataAction().doList(room));
			
			List<Room> roomList=roomDao.listRoom(room);
			
			Integer count=roomDao.listRoomCount(room);
			
			page=new PageBean(roomList,count,room.getCurentPage());
			System.out.println("333333333333333"+room.getDynamicFields().size());
			
			
		}catch(Exception e){
			e.printStackTrace();
			log.error("!", e);
		}
		return SUCCESS;
	}
	
	public String doQuery(){
		RoomDao roomDao=(RoomDao)getBean("roomDao");
		room=roomDao.findRoom(roomId);
		return SUCCESS;
	}
	
	
	
	public String doDetail(){
		RoomDao roomDao=(RoomDao)getBean("roomDao");
		room=roomDao.findRoom(roomId);
		
		return SUCCESS;
	}
	
	public String doUpdate(){
		RoomDao roomDao=(RoomDao)getBean("roomDao");
		
		roomDao.updateRoom(room);
		
		return doList();
	}
	
	
	public String doInsert(){
		RoomDao roomDao=(RoomDao)getBean("roomDao");
		roomDao.insertRoom(room);
		
		return doList();
	}
	

	
	public String doDelete(){
		RoomDao roomDao=(RoomDao)getBean("roomDao");
		roomDao.deleteRoom(room);
		room.clear(room.getClass(), room);
		
		return doList();
	}

	public Room getRoom() {
		return this.room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	public Integer getRoomId() {
		return this.roomId;
	}

	public void setRoomId(Integer roomId) {
		this.roomId = roomId;
	}

	public PageBean<Room> getPage() {
		return this.page;
	}

	public void setPage(PageBean<Room> page) {
		this.page = page;
	}
	
}
